import { ref } from 'vue'

export interface SearchFormProps {
    title?: string
    columns: Array<columnsType>
    isShowCollapse?: boolean
    autoSearch?: boolean
    columnNum?: number
    searchPermission?: Array<string>
    uniqueKey?: string
    showCollapse?: boolean
    listSettingFunc?: Function
    labelWidth?: string|number
    labelPosition?: 'top' | 'left'|'right'
}
// interface columnsType {
//   prop: string
//   label: string
//   isFilter?: boolean
//   filterConfig: {
//     type: string
//     placeholder?: string
//     options?: Array<{ label: string; value: any }>
//     multiple?: boolean
//     appendData?: (result: any[]) => any[]
//     [key: string]: any
//   }
// }

export interface columnsType {
  prop: string
  label: string
  isFilter?: boolean // 是否显示在查询条件中
  filterConfig: {
    type: string
    placeholder?: string
    options?: Array<{ label: string; value: any }>
    multiple?: boolean
    hiddenFilter?: boolean // 是否隐藏高级筛选
    defaultOperator?: string // 高级筛选默认查询条件
    appendData?: (result: any[]) => any[]
    [key: string]: any
  },
  getListSettingFunc?: Function
  saveListSettingFunc?: Function
}

export interface FilterConfig {
  type: string
  placeholder: string
  multiple?: boolean
  min?: number
  max?: number
  marks?: Record<number, string>
  unit?: string
  options?: Array<{
    label: string
    value: string | number
  }>
}

export interface FilterCondition {
  field: string
  operator: string
  value: any
}

export interface FilterResult {
  field: string
  include?: FilterCondition[]
  exclude?: FilterCondition[]
}

export function useAdvancedFilter() {
  // 存储所有高级筛选组件的引用
  const advancedFilterRefs = ref<Record<string, any>>({})

  // 筛选相关状态
  const activeFilters = ref<Record<string, boolean>>({})

  // 打开高级筛选弹窗
  const openAdvancedFilter = (prop: string) => {
    const filterRef = advancedFilterRefs.value[prop]
    if (filterRef && typeof filterRef.openDialog === 'function') {
      filterRef.openDialog()
    }
  }

  // 筛选条件记录
  const queryFilters = ref<any>({})
  // 表格筛选条件记录
  const tableFilters = ref<any>({})

  // 应用筛选
  const applyFilter = (filters: FilterResult) => {
    // 更新筛选状态
    activeFilters.value[filters.field] = !!(
      filters.exclude?.some((item) => item.value || item.value === 0) ||
      filters.include?.some((item) => item.value || item.value === 0)
    )
  }

  // 清除筛选
  const clearFilter = (prop: string) => {
    activeFilters.value[prop] = false
  }

  // 检查筛选是否激活
  const isFilterActive = (prop: string): boolean => {
    return activeFilters.value[prop]
  }

  // 处理高级筛选结果
  const handleAdvancedFilterChange = (filters: FilterResult, sourceToTable = false) => {
    if (sourceToTable) {
      // 这是表头筛选结果记录：额外修改筛选图标状态
      applyFilter(filters)
      tableFilters.value[filters?.field??''] = filters
    } else {
      // 这是查询筛选结果记录
      queryFilters.value[filters?.field??''] = filters
    }
    return filters
  }
  return {
    queryFilters,
    tableFilters,
    advancedFilterRefs,
    activeFilters,
    openAdvancedFilter,
    applyFilter,
    clearFilter,
    isFilterActive,
    handleAdvancedFilterChange
  }
}
